//
//  DateSelectViewController.m
//  WorkingResult
//
//  Created by 河野 智遵 on 11/09/04.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DateSelectViewController.h"


@implementation DateSelectViewController

@synthesize dateSelectActionTarget;
@synthesize dateSelectAction;

- (UIDatePicker*)getDatePicker
{
    if (datePicker_ == nil)
    {
        datePicker_ = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 10, 250)];
        datePicker_.datePickerMode = UIDatePickerModeDate;
        datePicker_.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    }
    return datePicker_;
}

- (UIButton*)getTodayButton
{
    UIButton* ret = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ret.frame = CGRectMake(0, 0, 100, 46);
    ret.center = CGPointMake([self getDatePicker].center.x, [self getDatePicker].frame.size.height + ret.frame.size.height);
    [ret setTitle:@"今日" forState:UIControlStateNormal];
    [ret addTarget:self action:@selector(onTodayButton) forControlEvents:UIControlEventTouchDown];
    return ret;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onDoneItem)];
    UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancelItem)];
    
    [self.navigationItem setRightBarButtonItem:rightItem animated:YES];
    [self.navigationItem setLeftBarButtonItem:leftItem animated:YES];

    [self.view addSubview:[self getDatePicker]];
    [self.view addSubview:[self getTodayButton]];
    
    [self setSelectedDate:[NSDate date]];
    self.title = @"日付の選択";
}

- (void)onCancelItem
{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (void)onDoneItem
{
    self.selectedDate = datePicker_.date;
    [self.dateSelectActionTarget performSelector:dateSelectAction];
    [self onCancelItem];
}

- (void)onTodayButton
{
    [self getDatePicker].date = [NSDate date];
    [self onDoneItem];
}

- (void)setSelectedDate:(NSDate *)selectedDate
{
    [self getDatePicker].date = selectedDate;
}

- (NSDate*)getSelectedDate
{
    return [[datePicker_.date copy] autorelease];
}


- (void)onCloseButton
{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (void)dealloc
{
    [datePicker_ release];
    [todayButton_ release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
